package com.ruoyi.finance.service.impl;

import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.finance.domain.AccountsPayable;
import com.ruoyi.finance.domain.Payment;
import com.ruoyi.finance.mapper.AccountsPayableMapper;
import com.ruoyi.finance.mapper.PaymentMapper;
import com.ruoyi.finance.service.IPaymentService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

/**
 * 付款Service业务层处理
 *
 * @author liuyu
 * @date 2024-11-04
 */
@Service
public class PaymentServiceImpl implements IPaymentService {
    @Resource
    private PaymentMapper paymentMapper;

    @Resource
    private AccountsPayableMapper payableMapper;

    /**
     * 查询付款
     *
     * @param id 付款主键
     * @return 付款
     */
    @Override
    public List<Payment> selectPaymentById(Long id) {
        return paymentMapper.selectPaymentById(id);
    }

    /**
     * 查询付款列表
     *
     * @param payment 付款
     * @return 付款
     */
    @Override
    public List<Payment> selectPaymentList(Payment payment) {
        return paymentMapper.selectPaymentList(payment);
    }

    /**
     * 新增付款
     *
     * @param payment 付款
     * @return 结果
     */
    @Override
    public int insertPayment(Payment payment) {
        //获取已付款总额
        BigDecimal amount = selectSumAmount(payment.getAccountsPayableId()) == null ? BigDecimal.valueOf(0) : selectSumAmount(payment.getAccountsPayableId());
        System.out.println(amount);
        //获取需付款总额
        AccountsPayable accountsPayable = payableMapper.selectAccountsPayableById(payment.getAccountsPayableId());
        if (payment.getPayAmount().add(amount).compareTo(accountsPayable.getAmount()) > 0) {
            throw new ServiceException("业务异常:总付款金额超过应付金额");
        }

        paymentMapper.insertPayment(payment);
        updateStatus(payment,1);
        return 1;
    }

    /**
     * 修改付款
     *
     * @param payment 付款
     * @return 结果
     */
    @Override
    public int updatePayment(Payment payment) {

        //获取已付款总额
        BigDecimal amount = selectSumAmount(payment.getAccountsPayableId()) == null ? BigDecimal.valueOf(0) : selectSumAmount(payment.getAccountsPayableId());
        System.out.println(amount);
        //获取需付款总额
        AccountsPayable accountsPayable = payableMapper.selectAccountsPayableById(payment.getAccountsPayableId());
        if (payment.getPayAmount().add(amount).compareTo(accountsPayable.getAmount()) > 0) {
            throw new ServiceException("业务异常:总付款金额超过应付金额");
        }
        paymentMapper.updatePayment(payment);
        updateStatus(payment,0);
        return 1;
    }


    /**
     * 根据id获取付款记录
     *
     * @return
     */
    @Override
    public Payment getBillingById(Long id) {
        return paymentMapper.getBillingById(id);
    }

    /**
     * 获取付款总额
     *
     * @return
     */
    @Override
    public BigDecimal selectSumAmount(Long accountsPayableId) {
        return paymentMapper.selectSumAmount(accountsPayableId);
    }


    /***
     *
     * @param payment
     * @param k 修改或新增  1新增
     */
    public void updateStatus(Payment payment, int k) {
        //获取已付款总额
        BigDecimal amount = selectSumAmount(payment.getAccountsPayableId());
        //获取需付款总额
        AccountsPayable accountsPayable = payableMapper.selectAccountsPayableById(payment.getAccountsPayableId());

        //新建一个付款表对象
        AccountsPayable accountsPayable1 = new AccountsPayable();

        //如果是新增
        if (k == 1) {
            //已付款总额小于 需付款总额  将状态改为部分支付
            if (amount.compareTo(accountsPayable.getAmount()) < 0) {
                accountsPayable1.setId(payment.getAccountsPayableId());
                accountsPayable1.setStatus(2L);
            }else {
                accountsPayable1.setId(payment.getAccountsPayableId());
                accountsPayable1.setStatus(1L);
            }
            payableMapper.updateAccountsPayable(accountsPayable1);
        } else {
            if (amount.compareTo(accountsPayable.getAmount()) >= 0 && accountsPayable.getStatus() != 1) {
                accountsPayable1.setStatus(1L);
                accountsPayable1.setId(payment.getAccountsPayableId());
                payableMapper.updateAccountsPayable(accountsPayable1);
            }
        }
    }
}
